package Dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Intnum;
import entity.Longnum;
import entity.Record;
import entity.Setting;
import entity.Str;
import entity.Test;
import entity.Word;
import service.TestServiceimpl;
import util.Dbconnect;

public class TestDaoimpl implements TestDao {
	SetDao sd = new SetDaoimpl();
    /**
	 * Title: randomCommon
	 * Description:获取随机数
	 * @author 毛立新
	 * @return
	 * @throws Exception 
	 */
	public List<Integer> randomCommon(List<Integer> ls,String email) throws Exception  {
		List<Integer> integers = new ArrayList<Integer>();
		int mx = Math.min(10,ls.size());
		for (int i = mx; i>0;) {
            int randomNumber = (int) (Math.random()*ls.size());
            //如果此数已重复
            if (integers.contains(randomNumber)) {
                 continue;
            } else {
            	i--;
                integers.add(randomNumber);
            }
            
        }
        List<Integer> in = new ArrayList<Integer>();
		for (int i = mx; i>0;i--) {
			in.add(ls.get(integers.get(i-1)));
        }
         return in;
	}
	
	/**
	 * Title: getlist
	 * Description:获取已背单词wordID列表
	 * @author 毛立新
	 * @param email
	 * @return
	 * @throws Exception
	 */
	public List<Integer> getlist(String email) throws Exception  {
		List<Integer> integers = new ArrayList<Integer>();
		String sql = "select wordid num from record where email=?";
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1, email);
		List<Intnum> query = Dbconnect.query(st,Intnum.class);
		Dbconnect.close(st);
		for(Intnum itm:query) {
			integers.add(itm.getNum());
		}
		return integers;
	}
	 /**
    	* Title: showByWord
    	* Description:按ID获取单词
    	* @author 毛立新
        *
     */
	@Override
	public String showByWord(int id) throws Exception {
		// TODO Auto-generated method stub
		String sql = "select mean_cn str from allword where id=?";
		PreparedStatement st = Dbconnect.init(sql);
		st.setInt(1,id);
		Str queryone = Dbconnect.queryone(st,Str.class);
		Dbconnect.close(st);
		return queryone.getStr();
	}
	
    /**
     * 
    	* Title: Getscore
    	* Description:获得前端分数
    	* @author 毛立新
        *
     */
	@Override
	public Integer Getscore(String email) throws Exception {
		// TODO Auto-generated method stub
		String sql = "select score num from test where email = ?";
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1, email);
		Intnum queryone = Dbconnect.queryone(st, Intnum.class);
		Dbconnect.close(st);
	    return queryone.getNum();
	}
	
	/**
	 * Title: updatescore
	 * Description:分数的上传
	 * @author 毛立新
	 * @param email
	 * @param score
	 * @return
	 * @throws Exception
	 */
	public void updatescore (String email,int score) throws Exception {
		    // TODO Auto-generated method stub
		    String sql = "update test set score = ? where email=?";
		    PreparedStatement st = Dbconnect.init(sql);
			st.setInt(1, score);
			st.setString(2, email);
			int update2 = Dbconnect.update(st);
			System.out.println("已修改");
	}
	/**
	 * Title: deletescore
	 * Description:删除score
	 * @author 毛立新
	 * @param email
	 * @throws Exception
	 */
	public void deletescore(String email)throws Exception{
		    String sql = "update test set score=NULL WHERE email=?";
			PreparedStatement st = Dbconnect.init(sql);
			st.setString(1, email);
			int update = Dbconnect.update(st);
			System.out.println("已删除");
	}
	/**
	 * Title: addtest
	 * Description:添加记录
	 * @author 毛立新
	 * @param email
	 * @param score
	 * @return
	 * @throws Exception
	 */
	public int addtest(String email,int score)throws Exception{
		String sql = "insert into test values(?,?)";
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1,email);
		st.setInt(2,score);
		int update = Dbconnect.update(st);
		System.out.println("已上传");
		return update;
	}
	 /**
     * Title: isaddorupdate
     * Description:判断是修改信息还是上传信息
     * @author 毛立新
     * @throws Exception
     */
    public boolean isaddorupdate(String email,int score)throws Exception {
    	String sql = "select * from test where email=?";
    	PreparedStatement st = Dbconnect.init(sql);
    	st.setString(1, email);
    	Test queryone = Dbconnect.queryone(st,Test.class);
    	if(queryone==null) {
    		new TestServiceimpl().addtest(email, score);
    		return false;
    	}
    	new TestServiceimpl().updatescore(email, score);
    	return true;
    	
    }
	

}
